/*jslint sloppy: true */
/*global require */
var LM = require('../global');
var hljs = require('highlight.js/lib/highlight.js');
var angular = require('angular');
require('./modal.html');

LM.controller('Modal', function ($scope, $sce, ModalService) {
	$scope.show = false;

	ModalService.subscribe(function (line, message) {
		line = line - 1; // index
		$scope.show = true;

		var htmls = hljs.highlight('lemoncase', message, true).value.split('\n');

		htmls[line] = '\n<div class="bg-danger">' + htmls[line] + '</div>';

		$scope.code = $sce.trustAsHtml(htmls.join('\n'));
	});
}).service('ModalService', function () {
	var modalCb = angular.noop;

	function subscribe(fn){
		modalCb = fn;
	}

	function showModal(line, message){
		modalCb(line, message);
	}

	return {
		subscribe: subscribe,
		showModal: showModal
	};
});
